<!DOCTYPE html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>新建销售单</title>
  <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="../css/style.css" />
</head>

<body>
  <script src="../scripts/jquery-1.8.1.js" type="text/javascript"></script>
  <script src="../node_modules/react/dist/react.js" type="text/javascript"></script>
  <script src="../node_modules/react-dom/dist/react-dom.js" type="text/javascript"></script>
  <script src="../node_modules/immutable/dist/immutable.js" type="text/javascript"></script>
  <script src="../scripts/jquery.validate-1.11.1.js" type="text/javascript"></script>
  <script src="../scripts/modernizr-1.7.min.js" type="text/javascript"></script>
  <script src="../scripts/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
  <script src="../scripts/PropertyManager.js" type="text/javascript"></script>
  <script src="../scripts/SelectProducts.js" type="text/javascript"></script>
  <script src="../scripts/GetState.js" type="text/javascript"></script>
  <script src="../scripts/PERM.js" type="text/javascript"></script>
  <script src="../scripts/jquery_validator_message_cn.js" type="text/javascript"></script>
  <script src="../scripts/PageLoading.js" type="text/javascript"></script>
  <script src="../scripts/ChinesePY.js" type="text/javascript"></script>
  <script src="../scripts/jquery.autocomplete.min.js"></script>
  <div id="content"></div>
  <script type="text/javascript">
    var sel_products = [];
    function selectProduct(product) {
      //删除已有  
      for (var i = 0; i < sel_products.length; i++) {
        if (sel_products[i].id == product.id) {
          sel_products.splice(i, 1);
          break;
        };
      };
      sel_products.push(product);
    };

    function unSelectProduct(product) {
      //删除已有  
      for (var i = 0; i < sel_products.length; i++) {
        if (sel_products[i].id == product.id) {
          sel_products.splice(i, 1);
          break;
        };
      };
    };

    function getSelProductCount() {
      return sel_products.length;
    }

    $(function () {
      //InitOrder();

      //打印
      var rememberPrint = Number("${IsPrint}");;
      var isPrint = Number("${IsPrint}");
      var isToList = Number("${IsToList}");
      var printSaleId = Number("${SaleId}");


      if (isPrint == 1) {
        $("#chkPrint").attr("checked", "checked");
        $("#chkPrint").parent().addClass("lightGreenButton");
      } else if (isPrint == 0) {
        if (rememberPrint == 1) {
          $("#chkPrint").attr("checked", "checked");
          $("#chkPrint").parent().addClass("lightGreenButton");
        } else {
          $("#chkPrint").removeAttr("checked");
          $("#chkPrint").parent().removeClass("lightGreenButton");
        }
      }


      if (isToList == 1) {
        parent.childAddTab('销售历史', '/Sale/List?IsPrint=' + isPrint + '&SaleId=' + printSaleId + "&IsToList=" + isToList, '慧管货');
      } else
        if (isPrint == 1) {
          OpenPrint('/Sale/Print?SaleId=' + printSaleId);
        }
      GetClientAddress();

      // $('#ClientId').combobox({
      //     onChange: function (newValue, oldValue) {
      //         //alert(newValue);
      //         GetClientAddress();
      //     }
      // });
    });


    // 选择商品点击OK处理填充数据
    function SelectProductOK(products, index) {
      productView.selectProductOK(products, index);
    }


    //单行计算金额
    function calSaleAmt(obj) {
      //验证最低销售和最高销售价   
      var lowPrice = obj.get("lowSalePricePer");
      var salePrice = obj.get("productPrice");
      var saleCount = obj.get("productCount");
      var saleAmt = Number(salePrice) * Number(saleCount);
      obj.set("eachSaleAmt", saleAmt);

      /*
      //验证数量
      if (saleCount < 0) {
          Dialog.alert("商品数量不能小于0，请重新修改！");
          $(obj).parent().parent().children('td').eq(4).find('input').focus();
          return false;
      }*/
      /*
      //验证最低销售和最高销售价 
      if ($(obj).parent().parent().children('td').eq(4).find('input[name="SaleCount"]').val() != "0") {
          if (isCheckLowPrice == "1" && isNumber(lowPrice) && isNumber(salePrice) && Number(lowPrice) != 0 && Number(salePrice) < Number(lowPrice)) {
              Dialog.alert('当前销售价低于最低售价！', function () {
                  $(obj).parent().parent().children('td').eq(3).find('input').val("");
                  $(obj).parent().parent().children('td').eq(3).find('input').focus();
                  return;
              });
          };
      }   ;      */
    }


    //单行根据总金额和数量计算单价
    function calSalePrice(obj) {
      var saleCount = obj.get("productCount");
      var saleAmt = obj.get("eachSaleAmt");
      var salePrice = Number(saleAmt) / Number(saleCount);
      obj.set("productPrice", salePrice);
      calCountAmt();
    }

    //提交表单验证
    function ValidateForm() {
      return true;
    }


    function GetClientAddress() {
      $.ajax({
        dataType: "json",       
        url: "/clients/1",
        type: "GET",
        success: function (data) {
          $("#ReceAddress").val(data.clientAddress);
          $("#ClientLink").val(data.clientLink);
          $("#ClientTel").val(data.clientTel);
        },
        error: function () {
          //Dialog.alert("ssssssss");
        }
      });
    }


    function chkIsPrint() {
      var isNeed = document.getElementsByName("chkPrint")[0];
      if (isNeed.checked) {
        $(isNeed).parent().addClass("lightGreenButton");
      }
      else {
        $(isNeed).parent().removeClass("lightGreenButton");
      }
    }

    //显示和隐藏送货地址
    function chgDeliverGoods() {
      var isNeed = document.getElementsByName("IsNeedDeliverGoods")[0];
      if (isNeed.checked) {
        $("#deliverGoods").show();
        $(isNeed).parent().addClass("lightGreenButton");
        $("input[name='IsNeedDeliverGoods']").val(1);
      }
      else {
        $("#deliverGoods").hide();
        $(isNeed).parent().removeClass("lightGreenButton");
        $("input[name='IsNeedDeliverGoods']").val(0);
      }
    }



    function popAddProduct() {
      parent.$.extend(parent.$.messager.defaults, {
        ok: "添加商品",
        cancel: "取消"
      });
      parent.$.messager.confirm('系统提示', '商品列表中存在未知商品。您需要先把商品录入到库存中才能进行销售。', function (r) {
        if (r) {
          parent.childAddTab('新增商品', '/ProductInfo/Add', '基础资料');
        }
      });
      parent.$.extend(parent.$.messager.defaults, {
        ok: "确定",
        cancel: "取消"
      });
    }
  </script>
  </td>
  </tr>
  </tbody>
  </table>



  <script type="text/javascript">
    $(document).ready(function () {
      parent.$("#divLoading").hide();
      parent.$("#fullbg").hide();
      var errMsg = $(".validation-summary-errors").text();
      if (errMsg) {
        Dialog.alert(errMsg);
      }
    });
  </script>

  <script src="../dist/sale-add.js?"></script>
</body>